﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingRooms;
using B.S.Meeting.WriteAPI.Applications.Commands.Users;
using MD5Hash;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Yitter.IdGenerator;

namespace B.S.Meeting.WriteAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IMediator mediator;
        ILogger<UserController> logger;

        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="userPhone">手机号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<string>> SendCode([FromQuery] string userPhone)
        {
            try
            {
                logger.LogInformation($"开始向手机号：{userPhone}发送验证码");
                var command = new SendCodeCommand { Phone = userPhone };
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("发送验证码失败", ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddUser(User user)
        {
            try
            {
                AddUserCommand cmd = new AddUserCommand
                {
                    Data = user
                };
                logger.LogInformation($"添加用户：{user.UserName}信息，成功");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("添加用户信息失败",ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="command">重置密码参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> ResetPwd([FromBody] ResetPwdCommand command)
        {
            try
            {
                logger.LogInformation($"开始重置用户密码: {command.UserPhone}");
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "重置密码失败");
                throw;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> DelUser(int userId)
        {
            try
            {
                logger.LogInformation($"开始删除用户: {userId}");
                DelUserCommand command = new DelUserCommand { UserId = userId };
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除用户失败");
                throw;
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdateUser(User user)
        {
            UpdUserCommand cmd = new UpdUserCommand
            {
                Data = user,
            };
            var res = await mediator.Send(cmd);
            return res;
        }
    }
}
